package com.moyo.service;

import com.moyo.model.User;
import com.moyo.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 用户业务逻辑
 */
@Service
public class UserService {
    private final UserRepository userRepository = new UserRepository();
    private final SysPermissionService permissionService = new SysPermissionService();

    public User register(String username, String password, String phone, String email, Long operatorId) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setEmail(email);
        user.setEnabled(true);
        user.setVersion(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setCreatedBy(operatorId);
        user.setUpdatedBy(operatorId);
        return userRepository.save(user);
    }

    public Optional<User> login(String username, String password) {
        Optional<User> userOpt = userRepository.findByUsername(username);
        if (userOpt.isPresent()) {
            User user = userOpt.get();
            if (user.getPassword().equals(password) && Boolean.TRUE.equals(user.getEnabled())) {
                // 加载用户的角色和权限
                user.setRoles(permissionService.getUserRoles(user.getId()));
                user.setPermissions(permissionService.getUserPermissions(user.getId()));
                return Optional.of(user);
            }
        }
        return Optional.empty();
    }

    public List<User> listAll() {
        List<User> users = userRepository.findAll();
        // 加载每个用户的角色和权限
        users.forEach(user -> {
            user.setRoles(permissionService.getUserRoles(user.getId()));
            user.setPermissions(permissionService.getUserPermissions(user.getId()));
        });
        return users;
    }

    public Optional<User> findById(Long id) {
        Optional<User> userOpt = userRepository.findById(id);
        userOpt.ifPresent(user -> {
            user.setRoles(permissionService.getUserRoles(user.getId()));
            user.setPermissions(permissionService.getUserPermissions(user.getId()));
        });
        return userOpt;
    }

    public void disableUser(Long id, Long operatorId) {
        userRepository.findById(id).ifPresent(u -> {
            u.setEnabled(false);
            u.setUpdateTime(LocalDateTime.now());
            u.setUpdatedBy(operatorId);
            userRepository.save(u);
        });
    }

    public void enableUser(Long id, Long operatorId) {
        userRepository.findById(id).ifPresent(u -> {
            u.setEnabled(true);
            u.setUpdateTime(LocalDateTime.now());
            u.setUpdatedBy(operatorId);
            userRepository.save(u);
        });
    }

    /**
     * 分配角色
     */
    public void assignRoles(Long userId, List<Long> roleIds, Long operatorId) {
        permissionService.assignRolesToUser(userId, roleIds, operatorId);
    }
}
